// Grozi Ties
// David Ing and Jae Shin Woo
//
// This is a program meant to receive XY coordinates
//----------------------------------------------------------------

OscRecv recv; 

7000 => recv.port; // port that it listens from
recv.listen();
recv.event( "/edge, i" ) @=> OscEvent oe1;
recv.event( "/edge2, i") @=> OscEvent oe2;

// Received x and y positions
0 => int xPos;
0 => int yPos;

// Given cow coordinates
50 => int cowLowX;
60 => int cowHighX;
100 => int cowLowY;
120 => int cowHighY;


// run time of the program
while ( true ){ 
        // works with the X coordniates
    oe1 => now;
	// WHY DOESNT IT EVEN REACH THIS LINE OF CODE?
        <<<"AHHH">>>;

    // asks for next messge
    while ( oe1.nextMsg() != 0 ){
        oe1.getInt() => xPos;
        <<< "RECEIVING: ", xPos >>>;

    }
    
    // works with the Y coordinates
    oe2 => now;
    while ( oe2.nextMsg() != 0 ){
        oe1.getInt() => yPos;
        <<< "RECEIVING: ", yPos >>>;
    }
       
    // Processes the X and Y coordinates    
        if(xPos<cowLowX){
            <<<"sky">>>;
            SndBuf buf => Gain g => dac;
            "VOICE/sky.wav" => buf.read;
            .5 => g.gain;
            0 => buf.pos;
            Std.rand2f(.8,.9) => buf.gain;
            .2::second => now;
        }
        
        if(xPos>cowHighX){
            <<<"sky">>>;
            SndBuf buf => Gain g => dac;
            "VOICE/sky.wav" => buf.read;
            .5 => g.gain;
            0 => buf.pos;
            Std.rand2f(.8,.9) => buf.gain;
            .2::second => now;  
        }
        
        if((xPos<=cowHighX && xPos>=cowLowX) &&
             (yPos <= cowHighY && yPos >= cowLowY) ){
            <<< "cow" >>>;
            SndBuf buf => Gain r => dac;
            "VOICE/cow.wav" => buf.read;
            .5 => r.gain;
            0 => buf.pos;
            Std.rand2f(.8,.9) => buf.gain;
            .2::second => now;
        }
}
